
'use strict'
class suggestion{
    constructor () {

    }
    __mid(){// 启用token-verify中间件
        return [
          {
            name:'token-verify',
            path:['post']
          }
        ]
    }
    async post(c){
        let userId = c.box.user.userId;
        
        // 获取数据（还需要具体了解提交数据的具体结构）
        let data = JSON.parse(c.body);
        
        // 存入数据库
        let sqltext = `INSERT INTO `
                        +`suggestion("userId","suggestionContent","suggestionTime")`
                        +`VALUES ($1,$2)`;
        try {
            let r = await c.service.psql.query(sqltext,[userId,data.suggestionContent,new Date().getTime()])
            if (r.rowCount != 0) { //对是否添加建议成功做判断
                c.send('Submission successful!')
            } else {
                c.send('Submission failed!')
            }
        } catch (err) {
            c.status(500).send("Database's operations failed!");
        }
    }
}
module.exports = suggestion;